package com.ruoyi.cases.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cases.domain.YltCase;
import com.ruoyi.cases.service.IYltCaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 案件信息Controller
 * 
 * @author ruoyi
 * @date 2024-12-17
 */
@RestController
@RequestMapping("/caseManagement/cases")
public class YltCaseController extends BaseController {
    @Autowired
    private IYltCaseService yltCaseService;

    /**
     * 查询案件信息列表
     */
    @PreAuthorize("@ss.hasPermi('caseManagement:cases:list')")
    @GetMapping("/list")
    public TableDataInfo list(YltCase yltCase) {
        startPage();
        List<YltCase> list = yltCaseService.selectYltCaseList(yltCase);
        return getDataTable(list);
    }

    /**
     * 导出案件信息列表
     */
    @PreAuthorize("@ss.hasPermi('caseManagement:cases:export')")
    @Log(title = "案件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YltCase yltCase) {
        List<YltCase> list = yltCaseService.selectYltCaseList(yltCase);
        ExcelUtil<YltCase> util = new ExcelUtil<YltCase>(YltCase.class);
        util.exportExcel(response, list, "案件信息数据");
    }

    /**
     * 获取案件信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('caseManagement:cases:query')")
    @GetMapping(value = "/{caseId}")
    public AjaxResult getInfo(@PathVariable("caseId") Long caseId) {
        return success(yltCaseService.selectYltCaseByCaseId(caseId));
    }

    /**
     * 新增案件信息
     */
    @PreAuthorize("@ss.hasPermi('caseManagement:cases:add')")
    @Log(title = "案件信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YltCase yltCase) {
        YltCase checkCase = yltCaseService.checkCase(Long.valueOf(yltCase.getCaseUserId()));
        if (checkCase!=null){
            AjaxResult ajaxResult = new AjaxResult(1001, "添加失败！", checkCase);
            return ajaxResult;
        }else {
            return toAjax(yltCaseService.insertYltCase(yltCase));
        }
    }

    /**
     * 修改案件信息
     */
    @PreAuthorize("@ss.hasPermi('caseManagement:cases:edit')")
    @Log(title = "案件信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YltCase yltCase) {
        return toAjax(yltCaseService.updateYltCase(yltCase));
    }

    /**
     * 删除案件信息
     */
    @PreAuthorize("@ss.hasPermi('caseManagement:cases:remove')")
    @Log(title = "案件信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{caseIds}")
    public AjaxResult remove(@PathVariable Long[] caseIds) {
       return toAjax(yltCaseService.deleteYltCaseByCaseIds(caseIds));
    }
}
